package c.c.c.a.o;

import c.b.a.a.a;
import com.huawei.agconnect.exception.AGCServerException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.zip.GZIPOutputStream;

/* compiled from: MultipartUtility */
public class d {
    public final String a;
    public HttpURLConnection b;
    public DataOutputStream c;
    public GZIPOutputStream d;

    public d(String str, String str2, boolean z) throws IOException {
        str2 = "AAA";
        StringBuilder g = a.g(str2);
        g.append(System.currentTimeMillis());
        g.append(str2);
        str2 = g.toString();
        this.a = str2;
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
        this.b = httpURLConnection;
        httpURLConnection.setUseCaches(false);
        this.b.setDoOutput(true);
        this.b.setDoInput(true);
        this.b.setRequestMethod("POST");
        httpURLConnection = this.b;
        g = new StringBuilder();
        g.append("multipart/form-data; boundary=");
        g.append(str2);
        httpURLConnection.setRequestProperty("Content-Type", g.toString());
        if (z) {
            this.b.setRequestProperty("Content-Encoding", "gzip");
            this.d = new GZIPOutputStream(this.b.getOutputStream());
            return;
        }
        this.c = new DataOutputStream(this.b.getOutputStream());
    }

    public String a() throws IOException {
        ArrayList arrayList = new ArrayList();
        StringBuilder g = a.g("\r\n--");
        g.append(this.a);
        g.append("--");
        g.append("\r\n");
        this.d.write(g.toString().getBytes());
        this.d.finish();
        this.d.close();
        int responseCode = this.b.getResponseCode();
        if (responseCode == AGCServerException.OK) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.b.getInputStream()));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                }
                arrayList.add(readLine);
            }
            bufferedReader.close();
            this.b.disconnect();
            g = new StringBuilder();
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                g.append((String) it.next());
            }
            return g.toString();
        }
        throw new IOException(a.A("Server returned non-OK status: ", responseCode));
    }

    public void b(String str, File file) throws IOException {
        String name = file.getName();
        StringBuilder g = a.g("--");
        String str2 = "\r\n";
        a.y(g, this.a, str2, "Content-Disposition: form-data; name=\"", str);
        a.y(g, "\"; filename=\"", name, "\"", str2);
        g.append("Content-Transfer-Encoding: binary");
        g.append(str2);
        g.append(str2);
        this.d.write(g.toString().getBytes());
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bArr = new byte[8192];
        while (true) {
            int read = fileInputStream.read(bArr);
            if (read != -1) {
                this.d.write(bArr, 0, read);
            } else {
                fileInputStream.close();
                this.d.write(str2.getBytes());
                return;
            }
        }
    }

    public void c(String str, String str2) {
        StringBuilder g = a.g("--");
        String str3 = "\r\n";
        a.y(g, this.a, str3, "Content-Disposition: form-data; name=\"", str);
        a.y(g, "\"", str3, "Content-Type: text/plain; charset=", "UTF-8");
        a.y(g, str3, str3, str2, str3);
        try {
            this.d.write(g.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
